// MP3TagDialog.cpp: implementation of the CMP3TagDialog class.
//
//////////////////////////////////////////////////////////////////////

#include "MP3TagDialog.h"
#include "tagchanger.hrh"	// For resource IDs
#include <eikedwin.h>		// For CEikEdwin
#include <EIKENV.H>			// For envir
#include <avkon.hrh>
#include <eiktxlbx.h>		// For listbox
#include <eiktxlbm.h>		// For CTextListBoxModel
#include <aknpopupfieldtext.h> 	// For CAknPopupFieldText
#include <aknqueryvaluetext.h> // For CAknQueryValueTextArray
#include <barsread.h>			// For TResourceReader

#include <TagChanger.rsg>	// For resource definition

#include "MP3Tag.h"			// For CMP3Tag class

#define TAGNAMELIST _L("Title"), _L("Artist"), _L("Album"), _L("Year"), _L("Comment"), _L("Genre")

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//CMP3TagDialog::CMP3TagDialog()
//{


CMP3TagDialog::~CMP3TagDialog()
{
	// Alloc in heap, clean up
	for (int i=0; i<iTags->Count(); i++)
	{
		delete iTags->At(i);
	}

	if (iQueryVal)
	{
		delete iQueryVal;
		iQueryVal = NULL;
	}

	if (iTxtArray)
	{
		delete iTxtArray;
		iTxtArray = NULL;
	}

	if (iGenreList)
	{
		delete iGenreList;
		iGenreList = NULL;
	}

	if (iCommValue)
	{
		delete iCommValue;
		iCommValue = NULL;
	}
}

void CMP3TagDialog::PreLayoutDynInitL()
{
	//CEikTextListBox* listbox=(CEikTextListBox*)Control(ETagChanger_MP3TAG_INFOR_Genre);
	//CDesCArray* items = static_cast<CDesCArray*>(listbox->Model()->ItemTextArray());
	// Set Genre value, fill popup list first
	/*
	CAknPopupField* PopupField = (CAknPopupField*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Genre);
		
		if (PopupField)	// Get OK
		{
			//TResourceReader RReader;
	
			//RReader.AllocReadLC();
			//RReader.ReadDesCArrayL();
			// Test code
			// Create example dynamic list for the popup field.
			iGenreList = new (ELeave) CDesCArrayFlat( 18 );
			//CleanupStack::PushL( array );
			iGenreList->AppendL( _L("January") );
			iGenreList->AppendL( _L("February") );
			iGenreList->AppendL( _L("March") );
			iGenreList->AppendL( _L("April") );
			iGenreList->AppendL( _L("May") );
			iGenreList->AppendL( _L("June") );
	
			iTxtArray = CAknQueryValueTextArray::NewL();
			iTxtArray->SetArray( *iGenreList );
			iQueryVal = CAknQueryValueText::NewL();
			iQueryVal->SetArrayL( iTxtArray );
			iQueryVal->SetCurrentValueIndex( 0 );
			//		
			//		
			PopupField->SetQueryValueL(iQueryVal);
		}*/
	
}

void CMP3TagDialog::ConstructL(CArrayFixFlat<MMediaTag*>& aTags, TBool aEditable)
{	
	CTagDialog::ConstructL(aTags, aEditable);
	
	TBuf<TAGID3V1_MAXLEN> Value, Name;
	TBuf<TAGID3V1_MAXLEN> TagInfo[TAGID3V1_COUNT];
	
	// Get command tag display string
	iCommValue = CEikonEnv::Static()->AllocReadResourceL(R_TAGCHANGER_TAG_COMMON_VALUE);

	for (int i=0; i<aTags.Count(); i++)
	{
		MMediaTag* pTag = aTags[i];

		for (int j=0; j<pTag->GetTagCount(); j++)
		{
			if (TagInfo[j] == iCommValue->Des())		// assigned as different
			{
				continue;
			}
			else
			{
				// Get tag data
				if (pTag->GetTagInfo(j, Name, Value))
				{
					// Error Handler
					User::Leave(100);
				}

				RDebug::Print(TagInfo[j]);
				RDebug::Print(Value);

				if (TagInfo[j].Length() != 0)	// Hold content, compare
				{
					if (TagInfo[j].Compare(Value) != 0)
					{
						TagInfo[j] = iCommValue->Des();
					}
				}
				else							// Empty assign
				{
					TagInfo[j] = Value;
				}
			}
		}
	}

	for (int k=0; k<TAGID3V1_COUNT-1; k++)	// -1 cause we use Popup Field instead of Editor
	{
		CEikEdwin* Edit = (CEikEdwin*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Title+k);
		
		if (Edit)
		{
			Edit->SetTextL(&TagInfo[k]);
		}
	}

	// Set Genre
	CAknPopupFieldText* PopupField = (CAknPopupFieldText*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Genre);
	
	if (PopupField)
	{
		PopupField->SetCurrentValueIndex(ValueToIndex(TagInfo[TAGID3V1_COUNT-1]));
	}
	
	
	// Update dialog
	UpdatePageL(ETrue);
	SetEditableL(aEditable);
}

TInt CMP3TagDialog::ValueToIndex(const TDesC& aValue)
{
	CAknPopupFieldText* PopupField = (CAknPopupFieldText*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Genre);

	TLex lex;
	TInt CurIndex;
	
	RDebug::Print(aValue);
	lex.Assign(aValue);
	lex.Val(CurIndex);
	
	if (CurIndex > 255 || CurIndex < 0)	// Unknown value
	{
		CurIndex = PopupField->MdcArray()->MdcaCount() - 1;
	}

	return CurIndex;
}

void CMP3TagDialog::IndexToValue(TDes& aValue)
{
	CAknPopupFieldText* PopupField = (CAknPopupFieldText*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Genre);

	if (aValue == iCommValue->Des())
	{
		aValue.Num(PopupField->MdcArray()->MdcaCount() - 1);
	}
	else
	{
		aValue.Num(PopupField->CurrentValueIndex());
	}
}

TBool CMP3TagDialog::OkToExitL(TInt aButtonId)
{
	if (iEditable && EAknSoftkeySave == aButtonId)
	{
		// Get date from Edit controls
		TBuf<TAGID3V1_MAXLEN> TagNames[TAGID3V1_COUNT] = { TAGNAMELIST };
		TBuf<TAGID3V1_MAXLEN> TagValue;
		CDesCArrayFlat* Names = new(ELeave) CDesCArrayFlat(TAGID3V1_COUNT);
		CDesCArrayFlat* Values = new(ELeave) CDesCArrayFlat(TAGID3V1_COUNT);
		
		CleanupStack::PushL(Names);
		CleanupStack::PushL(Values);
		
		for (int i=0; i<TAGID3V1_COUNT-1; i++)	// Except Genre
		{
			CEikEdwin* Edit = (CEikEdwin*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Title+i);
			
			if (Edit)
			{
				Edit->GetText(TagValue);

				RDebug::Print(TagNames[i]);
				RDebug::Print(TagValue);
				
				Names->AppendL(TagNames[i]);
				Values->AppendL(TagValue);
			}		
		}

		// Process Genre
		CAknPopupFieldText* PopupField = (CAknPopupFieldText*)ControlOrNull(ETagChanger_MP3TAG_INFOR_Genre);
		
		if (PopupField)
		{
			IndexToValue(TagValue);

			RDebug::Print(TagNames[TAGID3V1_COUNT-1]);
			RDebug::Print(TagValue);
			
			// Add to array
			Names->AppendL(TagNames[TAGID3V1_COUNT-1]);
			Values->AppendL(TagValue);
		}
		
		// Verify
		
		// Translate
		
		// Store
		RFs Session;
		User::LeaveIfError(Session.Connect());
		
		for (i=0; i<iTags->Count(); i++)
		{
			MMediaTag* pTag = iTags->At(i);
			pTag->ChangeTagL(Session, *Names, *Values);
			
			//delete pTag;
		}
		
		// Clean up
		CleanupStack::PopAndDestroy(2);	// The two arraies
		Session.Close();
	}

	return ETrue;
}